package shop.data;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class ConnectionPool {
	private static ConnectionPool pool=null;
	private static DataSource datasource=null;
	private ConnectionPool(){
		try{
			InitialContext ic=new InitialContext();
			datasource=(DataSource)ic.lookup("java:/comp/env/jdbc/murach");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static ConnectionPool getInstance(){
		if(pool==null){
			pool=new ConnectionPool();
		}
		return pool;
	}
	public Connection getConnection(){
		try {
			return datasource.getConnection();
		} catch (SQLException sqle) {
			// TODO: handle exception
			sqle.printStackTrace();
			return null;
		}
	}
	public void freeConnection(Connection c){
		try {
			c.close();
		} catch (SQLException sqle) {
			// TODO: handle exception
			sqle.printStackTrace();
		}
	}
}
